var user = require('../../utils/user');
var util = require('../../utils/util');
var api = require('../../request/api')
const app = getApp();// 引入app
import Toast from '../../vant/weapp/toast/toast';

// pages/home/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showShade:false,
    noteList:[],
    note:{},
    orderList:[],
    activeNames: ['1'],
    showPopup:false,
    popup:'',
    value:''
  },
  onClick(){
    this.setData({ showShade: true });
    Toast.loading({
      duration: 0,
      message: '加载中...',
      forbidClick: true,
    });
    util.post(api.order.qryKeyword,{payStatus:2,orderStatus:1,keyword:this.data.value}).then(res =>{
      Toast.clear();
      this.setData({ showShade: false });
      if(res.code == 200){
        this.setData({
          orderList:res.data.list
        })
      }else{
        Toast.fail(res.message)
      }
    }).catch(()=>{
      Toast.clear();
      this.setData({ showShade: false });
    });
  },
  getNotice(){
    this.setData({ showShade: true });
    Toast.loading({
      duration: 0,
      message: '加载中...',
      forbidClick: true,
    });
    util.post(api.note.query,{}).then(res => {
      Toast.clear();
      this.setData({ showShade: false });
      if(res.code == 200){
        this.setData({
          noteList:res.data.list,
          note:res.data.list[0]
        })
      }else{
        Toast.fail(res.message)
      }
    }).catch(()=>{
      Toast.clear();
      this.setData({ showShade: false });
    });
  },
  getOrder(){
    this.setData({ showShade: true });
    Toast.loading({
      duration: 0,
      message: '加载中...',
      forbidClick: true,
    });
    util.post(api.order.query,{queryObj:{payStatus:2,orderStatus:1,orderBy:'startTime'}}).then(res => {
      Toast.clear();
      this.setData({ showShade: false });
      if(res.code == 200){
        this.setData({
          orderList:res.data.list
        })
      }else{
        Toast.fail(res.message)
      }
    }).catch(()=>{
      Toast.clear();
      this.setData({ showShade: false });
    });
  },
  getOrderDetail(e){
    var detail = e.currentTarget.dataset.variable;
    this.setData({ 
      showPopup: true,
      popup:detail 
    });
  },
  popupClose() {
    this.setData({ showPopup: false });
  },
  collapseChange(event) {
    this.setData({
      activeNames: event.detail,
    });
  },
  appendToPing(e){
    var detail = e.currentTarget.dataset.variable;
    var info = {};
    info.orderId = detail.orderId;
    info.userId = wx.getStorageSync('userInfo').userId;
    this.setData({ showShade: true });
    Toast.loading({
      duration: 0,
      message: '加载中...',
      forbidClick: true,
    });
    util.post(api.confirm.append,info).then(res => {
      Toast.clear();
      this.setData({ showShade: false });
      if(res.code == 200){
        Toast.success(res.message);
      }else{
        Toast.fail(res.message);
      }
    }).catch(()=>{
      Toast.clear();
      this.setData({ showShade: false });
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log( wx.getStorageSync('hasUserInfo'))
    this.getNotice();
    this.getOrder();
  },
  test: function(){
    wx.navigateTo({
      url: "/pages/login/login"
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})